﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.cms.presentation.Trees;
using System.Text;
using umbraco.interfaces;
using Procure.Data.Context;
using umbraco.BusinessLogic.Actions;

namespace Procure.Web.attribute
{
    public class loadAttribute : BaseTree
    {
        public loadAttribute(string application) : base(application) { }

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.Icon = FolderIcon;
            rootNode.OpenIcon = FolderIconOpen;
            rootNode.NodeType = TreeAlias;
            rootNode.NodeID = "init";


        }

        protected override void CreateRootNodeActions(ref List<IAction> actions)
        {
            actions.Clear();
            actions.Add(ActionNew.Instance);
            actions.Add(ActionRefresh.Instance);
        }

        protected override void CreateAllowedActions(ref List<IAction> actions)
        {
            actions.Clear();
            //actions.Add(ActionRefresh.Instance);
            actions.Add(ActionDelete.Instance);

        }

        public override void Render(ref XmlTree tree)
        {

            XmlTreeNode xNode = null;
            try
            {
                ProcureDataContext dataStore = DataFactory.GetDataContext();
                var attributes = from attribute in dataStore.Attributes
                                   orderby attribute.Name
                                   select attribute;

                foreach (var attribute in attributes)
                {
                    xNode = XmlTreeNode.Create(this);
                    xNode.NodeID = attribute.Id.ToString();
                    xNode.Text = attribute.Name;
                    xNode.Action = string.Format("javascript:openAttribute('{0}');", attribute.Id);
                    xNode.Icon = "attribute.gif";
                    tree.Add(xNode);
                }
            }
            catch (Exception) { }
        }

        public override void RenderJS(ref StringBuilder Javascript)
        {
            Javascript.Append(
               @"
                    function openAttribute(id) {
	                    parent.right.document.location.href = 'Procure/editAttribute.aspx?id=' + id;
                    }
                ");
        }

    }
}